package mmc.server.shell.ops;

import mmc.server.shell.OperationNotAcceptedException;
import mmc.server.shell.ShellException;

public class MMCClass extends Operation{

	Class<?> theClass;
	Object theInstance;
	
	
	protected static String[] packageNames = {
		"mmc.server",
		"mmc.server.shell",
		"mmc.server.shell.ops",
		"mmc.server.shell.ops.baseops",
		"mmc.server.shell.ops.constant",
		"mmc.server.shell.ops.function",
		"mmc.server.shell.ops.utility",
		"mmc.server.shell.ui",
		"mmc.server.shell.util",
		"mmc",
		"mmc.action",
		"mmc.skill",
		"mmc.cell",
		"mmc.cell3",
		"mmc.gl.shader",
		"mmc.math.spline",
		"mmc.networking",
		"mmc.networking.data",
		"mmc.objects",
		"mmc.physics",
		"mmc.resource",
		"mmc.resource.config",
		"mmc.resource.data",
		"mmc.resource.models",
		"mmc.resource.models.collada",
		"java.lang"
		}; 
	
    public MMCClass(String text) throws ShellException {
        super(text = text.trim());
        
        if(!text.matches("[a-zA-z0-9\\.]*"))
        	throw new OperationNotAcceptedException(false, text + " is not a valid class name!");
        
        for(String packageName : packageNames){
        	if((theClass=searchPackage(packageName))!=null)
        		break;
        }
        
        if(theClass!=null)
        	return;
        
        try{
        	theClass=Class.forName(text);
        }catch(Throwable t){}
        
        if(theClass!=null)
        	return;
        /*
        searchPlayers();
        
        if(theClass!=null)
        	return;*/
        
        throw new OperationNotAcceptedException(false, text + " is not a valid class!");
    }
    /*
    protected void searchPlayers(){
    	for(Player p : Server.instance.getScene().getPlayers()){
    		if(p.getName().equalsIgnoreCase(text)){
    			theClass = Player.class;
    			theInstance = p;
    			return;
    		}
    	}
    }*/
    
    protected Class<?> searchPackage(String packageName){
    	try{
    		return Class.forName(packageName+"."+text);
    	}catch(Throwable t){}
    	
    	return null;
    }
    
    @Override
    public double execute() {
    	throw new RuntimeException("no primitive Output possible");
    }
    
    public static String getDescription(){
    	return formatHelpString("class","a class (name is case sensitive!)");
    }
    
    public static PriorityType getPriorityType() {
        return PriorityType.UNARY_OP;
    }
    
    public Class<?> getOutputType(){
    	return theClass;
    }
    
    public Object executeComplex(){
    	return theInstance;
    }
    
    public boolean isStatic(){
    	return theInstance==null;
    }
    
    public static boolean hideInDescription(){
    	return true;
    }
}
